package com.lancoo.znbkxx.teacher.diagnosis

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.view.View
import com.github.mikephil.charting.components.MarkerView
import com.github.mikephil.charting.components.XAxis
import com.github.mikephil.charting.data.Entry
import com.github.mikephil.charting.data.LineData
import com.github.mikephil.charting.data.LineDataSet
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter
import com.github.mikephil.charting.highlight.Highlight
import com.lancoo.znbkxx.teacher.Ext
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.MyToast
import com.lancoo.znbkxx.teacher.base.BaseActivity
import com.lancoo.znbkxx.teacher.model.data.GrammarChange
import com.lancoo.znbkxx.teacher.net.BkApiMethods
import com.lancoo.znbkxx.teacher.utils.LancooUtils
import kotlinx.android.synthetic.main.activity_grammar_trail.*
import kotlinx.android.synthetic.main.activity_grammar_trail.iv_back
import kotlinx.android.synthetic.main.activity_grammar_trail.lineChart
import kotlinx.android.synthetic.main.item_achievement_markview.view.*
import java.math.BigDecimal
import java.math.RoundingMode
import java.net.ConnectException
import java.net.SocketTimeoutException
import java.util.concurrent.TimeoutException


/**
 *@author Admin
 *@date 2021/1/6
 *
 */
class GrammarTrailActivity : BaseActivity() {
    private val masterGrammar = mutableListOf<Entry>()
    private val xLabels = mutableListOf<String>()
    private val zsd by lazy { intent.getStringExtra("zsd") }
    private val stuID by lazy { intent.getStringExtra("stuID") }
    override fun setLayout(): Int {
        return R.layout.activity_grammar_trail
    }

    override fun initView() {
        iv_back.setOnClickListener { finish() }
        getStuGrammarRZPlate()
    }

    override fun isNeedToolBar(): Boolean {
        return false
    }

    @SuppressLint("CheckResult")
    fun getStuGrammarRZPlate() {
        val method = BkApiMethods(this)
        val params = BkApiMethods.Paramas()
        params["ZsdArea"] = zsd
        params["stuID"] = stuID
        method.getStuGrammarRZPlate(Ext.RZZD_URL, params).subscribe({
            if (it.Code == 1) {
                initLineChart(it.Data.GrammarChangeList)
            } else {
                MyToast.showToast( it.Message)
            }
        }, {
            val error: String = if (it is TimeoutException || it is SocketTimeoutException || it is ConnectException) {
                getString(R.string.aibk_network_timeout)
            } else {
                getString(R.string.aibk_server_sick)
            }
            MyToast.showToast( error)
        })
    }

    private fun initLineChart(grammarChangeList: List<GrammarChange>) {
        if (grammarChangeList.isEmpty()) {
            group2.visibility = View.INVISIBLE
            tv_empty.visibility = View.VISIBLE
            return
        }
        masterGrammar.clear()
        xLabels.clear()
        grammarChangeList.forEachIndexed { index, change ->
            val entry = Entry(index.toFloat(), change.Master.toFloat())
            masterGrammar.add(entry)
            xLabels.add(change.DayTime)
        }
        lineChart.apply {
            val dataSet = LineDataSet(masterGrammar, "").apply {
                color = Color.parseColor("#22d7bb")
                setCircleColor(Color.parseColor("#22d7bb"))
                lineWidth = 2f
                circleRadius = 2.5f
                setDrawCircleHole(true)
                circleHoleRadius = 1.5f
            }
            val lineData = LineData(dataSet)
            lineData.setDrawValues(false)
            data = lineData
            axisLeft.apply {
                textColor = Color.parseColor("#999999")
                spaceBottom = 0f
                axisMinimum = 0f
            }
            axisRight.isEnabled = false
            legend.isEnabled = false
            description.isEnabled = false
            setPinchZoom(false)
            setScaleEnabled(false)
            val markerView = MyMarkView(this@GrammarTrailActivity,
                    R.layout.item_achievement_markview)
            markerView.chartView = this
            marker = markerView
            xAxis.apply {
                axisMinimum = -0.5f
                axisMaximum = grammarChangeList.size.toFloat() + 0.5f
                position = XAxis.XAxisPosition.BOTTOM
                isGranularityEnabled = true
                labelCount = xLabels.size
                setDrawGridLines(false)
                textSize = 11f
                textColor = Color.parseColor("#999999")
                valueFormatter = IndexAxisValueFormatter(xLabels)
            }
            animateX(1000)
        }
    }

    inner class MyMarkView(context: Context, layoutResource: Int) :
            MarkerView(context, layoutResource) {
        @SuppressLint("SetTextI18n")
        override fun refreshContent(e: Entry?, highlight: Highlight?) {
            tv_hint.text = "语法掌握率:${BigDecimal(highlight!!.y.toDouble()).setScale(2,
                    RoundingMode.HALF_UP)}"
            super.refreshContent(e, highlight)
        }
    }
}